package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.ResourceDto;
import com.example.human_resource_management.entry.po.Resource;
import com.example.human_resource_management.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    // 员工查询本部门资源内容
    @GetMapping("list")
    public ResponseData<List<ResourceDto>> selectResourceList() {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        List<ResourceDto> resourceList = resourceService.selectResourceListByEmpId(empId);
        return ResponseData.ok(resourceList);
    }

    //管理员端

    // 查询所有资源
    @GetMapping("/all")
    public ResponseData<List<ResourceDto>> getAllResources() {
        List<ResourceDto> resources = resourceService.selectAllResources();
        return ResponseData.ok(resources);
    }

    // 添加资源
    @PostMapping("/add")
    public ResponseData<String> addResource(@RequestBody Resource resource) {
        boolean success = resourceService.addResource(resource);
        return success ? ResponseData.ok("添加资源成功") : ResponseData.warn("添加资源失败");
    }

    // 更新资源
    @PutMapping("/update")
    public ResponseData<String> updateResource(@RequestBody Resource resource) {
        boolean success = resourceService.updateResource(resource);
        return success ? ResponseData.ok("更新资源成功") : ResponseData.warn("更新资源失败");
    }

    // 删除资源
    @DeleteMapping("/delete/{resId}")
    public ResponseData<String> deleteResource(@PathVariable Long resId) {
        boolean success = resourceService.deleteResource(resId);
        return success ? ResponseData.ok("删除资源成功") : ResponseData.warn("删除资源失败");
    }





}
